import http from "@/http/index.ts"
import {Material, MaterialListParam, MaterialExportRequest, MaterialImportResult, MaterialPriceImportResult} from '@/api/material/type.ts'

// 新增物料
export const addApi = (data: Material) => {
    return http.post("/material/createMaterial", data)
}

// 修改物料
export const updateApi = (data: Material) => {
    return http.put("/material/updateMaterial", data)
}

// 删除物料
export const deleteApi = (id: number) => {
    return http.delete(`/material/deleteMaterial/${id}`)
}

// 分页条件查询列表
export const getListApi = (data: MaterialListParam) => {
    return http.get("/material/getMaterialList", data)
}

// 根据物料查询该物料的所有规格列表
export const getSpecificationSimpleListApi = (materialName: string) => {
    return http.get("/material/specificationList?materialName=" + materialName)
}

// 根据ID获取物料详情
export const getByIdApi = (id: number) => {
    return http.get(`/material/getMaterialById/${id}`)
}

// 数据导入
export const importApi = (data: FormData): Promise<MaterialImportResult> => {
    return http.post("/material/importMaterial", data, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 数据导出
export const exportApi = (data: MaterialListParam) => {
    return http.get("/material/exportMaterial", data, {
        responseType: 'blob'
    })
}
export const refreshMaterialApi = () => {
    return http.post("/material/refreshMaterial")
}


// 文件上传
export const uploadApi = (data: FormData) => {
    return http.post("/material/upload", data, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 自定义字段导出物料档案
export const exportCustomApi = (data: MaterialExportRequest) => {
    return http.post("/material/exportCustom", data, {
        responseType: 'blob'
    })
}

// 下载物料导入模板
export const downloadTemplateApi = () => {
    return http.get("/material/downloadTemplate", {}, {
        responseType: 'blob'
    })
}

// 获取可导出字段列表
export const getExportFieldsApi = () => {
    return http.get("/material/getExportFields")
}

// 根据物料信息获取所有明细信息
export const getMaterialDetialApi = (data: Material) => {
    return http.post("/material/getMaterialDetial", data)
}

/**
 * 调用后端修改工艺接口
 */
export const updateProcessApi = () => {
  return http.get('/material/updateProcess')
}

/**
 * 下载材料单价更新模板
 */
export const downloadPriceTemplateApi = () => {
  return http.get("/material/price/template", {}, {
    responseType: 'blob'
  })
}

/**
 * 导入材料单价
 */
export const importPriceApi = (data: FormData): Promise<{code: number, message: string, data: MaterialPriceImportResult}> => {
  return http.post("/material/price/import", data, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}
